package com.cuit.mimall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cuit.mimall.dto.SeckillProductVo;
import com.cuit.mimall.entity.SeckillProduct;
import com.cuit.mimall.exception.ExceptionEnum;
import com.cuit.mimall.exception.XmException;
import com.cuit.mimall.mapper.SeckillProductMapper;
import com.cuit.mimall.service.ISeckillProductService;
import com.cuit.mimall.service.ISeckillTimeService;
import com.cuit.mimall.util.BeanUtil;
import com.cuit.mimall.util.RedisCache;
import com.cuit.mimall.util.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TeaWu
 * @since 2023-07-04
 */
@Service
public class SeckillProductServiceImpl extends ServiceImpl<SeckillProductMapper, SeckillProduct> implements ISeckillProductService {
    private final ISeckillTimeService seckillTimeService;

    private final RedisCache redisCache;

    @Autowired
    public SeckillProductServiceImpl(ISeckillTimeService seckillTimeService, RedisCache redisCache) {
        this.seckillTimeService = seckillTimeService;
        this.redisCache = redisCache;
    }


    /**
     * 获取秒杀商品的结束时间
     *
     * @param seckillId 秒杀商品id
     * @return 秒杀商品的结束时间
     */
    @Override
    public Long getEndTime(String seckillId) {
        QueryWrapper<SeckillProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("seckill_id", seckillId);
        SeckillProduct seckillProduct = this.getOne(queryWrapper);
        return seckillTimeService.getById(seckillProduct.getTimeId()).getEndTime();
    }

    /**
     * @param seckillProduct 秒杀商品
     * @return 秒杀商品
     */
    @Override
    public SeckillProduct getOne(SeckillProduct seckillProduct) {
        QueryWrapper<SeckillProduct> queryWrapper = new QueryWrapper<>(seckillProduct);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * @param seckillId 秒杀商品id
     */
    @Override
    public void decrStock(Integer seckillId) {
        baseMapper.decrStock(seckillId);
    }

    private final HashMap<String, Boolean> localOverMap = new HashMap<>();

    /**
     * 开始秒杀
     *
     * @param seckillId 秒杀商品id
     * @param userId    用户id
     */
    @Override
    public void seckillProduct(String seckillId, Integer userId) {
        if (localOverMap.get(seckillId) != null && localOverMap.get(seckillId)) {
            // 售空
            throw new XmException(ExceptionEnum.GET_SECKILL_IS_OVER);
        }
        // 判断秒杀是否开始, 防止路径暴露被刷
        Map<String, Object> m = redisCache.getCacheMap(RedisKey.SECKILL_PRODUCT + seckillId);
        if (!m.isEmpty()) {
            SeckillProductVo seckillProductVo = null;
            try {
                seckillProductVo = BeanUtil.map2bean(m, SeckillProductVo.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 秒杀开始时间
            Long startTime = Objects.requireNonNull(seckillProductVo).getStartTime();

            if (startTime > new Date().getTime()) {
                throw new XmException(ExceptionEnum.GET_SECKILL_IS_NOT_START);
            }
        }
    }

    /**
     *
     */
    @Override
    public void deleteAll() {
        baseMapper.deleteAll();
    }
}
